﻿# -*- coding UTF-8 -*-
from kivy.app import App
from kivy.properties import BoundedNumericProperty, StringProperty

import model.gamestate

from ui.hat_widget import HatWidget

from constants import MAXTIME, MAXWORDS, MINTIME, MINWORDS, DEFAULTTIME,\
    DEFAULTWORDS


class GameConfig(HatWidget):
    round_time_sec = BoundedNumericProperty(
        0, min=MINTIME, max=MAXTIME,
        errorhandler=lambda round_time_sec:
        MAXTIME if round_time_sec > MAXTIME else MINTIME)
    words_count = BoundedNumericProperty(
        0, min=MINWORDS, max=MAXWORDS,
        errorhandler=lambda words_count:
        MAXWORDS if words_count > MAXWORDS else MINWORDS)

    words_count_text = StringProperty()
    time_amount_text = StringProperty()

    def __init__(self, gamestate, presenter, **kwargs):
        """Making display with game settings

        need:
            gamestate.settings(if != None)
        do:
            change settings.time_per_round_sec
            & settings.words_number
        args:
            gamestate: GameState
            presenter: Presenter
        """
        self.presenter = presenter
        self.gamestate = gamestate        
        super(GameConfig, self).__init__(**kwargs)
        if hasattr(gamestate, 'settings') and gamestate.settings is not None:
            self.round_time_sec = gamestate.settings.time_per_round_sec
            self.words_count = gamestate.settings.words_number
        else:
            gamestate.settings = model.gamestate.Settings()
            self.round_time_sec = DEFAULTTIME
            self.words_count = DEFAULTWORDS

    def next_view(self):
        """Changes phase to START_GAME

        call on release "Далее" button
        """
        self.gamestate.settings.words_number = self.words_count
        self.gamestate.settings.time_per_round_sec = self.round_time_sec
        self.presenter.change_phase(model.gamestate.Phase.START_GAME)

    def go_back(self):
        """Changes phase to START_GAME

        call on release "Далее" button
        """
        new_settings = model.gamestate.Settings(self.words_count,
                                                self.round_time_sec)
        self.gamestate.settings = new_settings
        self.gamestate.settings.words_number = self.words_count
        self.gamestate.settings.time_per_round_sec = self.round_time_sec
        self.presenter.change_phase(model.gamestate.Phase.WELCOME)

    def change_time(self, delta):
        """Increase time_per_round in the delta

        args:
            delta: int
        """
        self.round_time_sec = self.round_time_sec + delta
        self.time_amount_text = str(self.round_time_sec)

    def read_time(self, val):
        self.time_amount_text = val
        if val.isdigit():
            self.round_time_sec = int(val)
        self.time_amount_text = str(self.round_time_sec)

    def change_words_count(self, delta):
        """Increase words_count in the delta

        args:
            delta: int
        """
        self.words_count = self.words_count + delta
        self.words_count_text = str(self.words_count)

    def read_words_count(self, val):
        self.words_count_text = val
        if val.isdigit():
            self.words_count = int(val)
        self.words_count_text = str(self.words_count)


class GameConfigApp(App):
    def build(self):
        from model.gamestate import GameState
        gamestate = GameState(None, None, None,
                              None, None, None, None)
        sample = GameConfig(gamestate, None)
        return sample


if __name__ == '__main__':
    GameConfigApp().run()
